package xing.service.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import xing.application.service.page.ModuleContentService;
import xing.application.service.page.dto.ContentItem;
import xing.application.service.page.dto.ContentMoveVo;
import xing.domain.page.annotation.Access;
import xing.domain.page.enums.AccessType;
import xing.infrastructure.data.CallResult;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/********************************
 * 文 件 名 :ContentController.java
 * 创 建 人：陈天龙
 * 日    期：2017/8/9
 * 描   述：模块内容控制层
 * 版 本 号：v1.0.0
 ********************************/
@Api(value = "模块内容",description = "模块内容相关的RESTful Api")
@RestController
@RequestMapping("/content")
public class ContentController {

    @Autowired
    private ModuleContentService moduleContentService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 描    述：导入
     * 创 建 人：陈天龙
     * 日    期：2017/8/14
     * @param contentItem
     * @return CallResult
     */
    @Access(accessType=AccessType.ADMIN)
    @ApiOperation(value="导入内容", notes="导入内容",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(name = "contentItem", value = "内容表单类", required = true, dataType = "ContentItem")
    @PostMapping("/import")
    public CallResult loadApps(@RequestBody ContentItem contentItem,HttpServletRequest request){
        CallResult res = null;
        try {
            res = moduleContentService.importContent(contentItem);
        } catch (Exception e) {
            logger.error("导入失败",e);
            res = CallResult.error("导入失败");
        }
        return res;
    }

    /**
     * 描    述：新增内容
     * 创 建 人：陈天龙
     * 日    期：2017/8/9
     * @param contentItem
     * @return CallResult
     */
    @Access(accessType=AccessType.ADMIN)
    @ApiOperation(value="模块内容新增", notes="模块内容新增",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(name = "contentItem", value = "内容表单类", required = true, dataType = "ContentItem")
    @PostMapping("/new")
    public CallResult createContent(@RequestBody ContentItem contentItem){
        logger.info("企业"+contentItem.getCorpId()+"进入新增类型为"+contentItem.getType()+"内容的方法");
        CallResult res = null;
        try {
            res = moduleContentService.createContent(contentItem);
        } catch (Exception e) {
            logger.error("保存失败",e);
            res = CallResult.error("保存失败");
        }
        return res;
    }

    /**
     * 描    述：编辑内容
     * 创 建 人：陈天龙
     * 日    期：2017/9/5
     * @param contentItem
     * @return CallResult
     */
    @Access(accessType=AccessType.ADMIN)
    @ApiOperation(value="模块内容编辑", notes="模块内容编辑",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(name = "contentItem", value = "内容表单类", required = true, dataType = "ContentItem")
    @PostMapping("/edit")
    public CallResult editContent(@RequestBody ContentItem contentItem){
        logger.info("企业"+contentItem.getCorpId()+"进入编辑类型为"+contentItem.getType()+"内容的方法");
        CallResult res = null;
        try {
            res = moduleContentService.editContent(contentItem);
        } catch (Exception e) {
            logger.error("编辑失败",e);
            res = CallResult.error("编辑失败");
        }
        return res;
    }

    /**
     * 描   述：删除内容
     * 创 建 人：陈天龙
     * 日    期：2017/8/10
     * @param id
     * @return CallResult
     */
    @Access(accessType=AccessType.ADMIN)
    @ApiOperation(value="内容删除", notes="内容删除",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "内容id", required = true, dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "corpId", value = "企业号", required = true, dataType = "String",paramType = "path")})
    @DeleteMapping("/del/{corpId}/{id}")
    public CallResult deleteContent(@PathVariable("id") Long id,@PathVariable("corpId") String corpId){
        CallResult res = null;
        try {
            res = moduleContentService.deleteContent(id,corpId);
        } catch (Exception e) {
            logger.error("删除失败",e);
            res = CallResult.error("删除失败");
        }
        return res;
    }

    /**
     * 描   述：移除内容
     * 创 建 人：陈天龙
     * 日    期：2017/8/10
     * @param id
     * @return CallResult
     */
    @Access(accessType=AccessType.ADMIN)
    @ApiOperation(value="内容移除", notes="内容移除",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(name = "id", value = "模块内容关系id", required = true, dataType = "Long",paramType = "path")
    @DeleteMapping("/remove/{id}")
    public CallResult removeContent(@PathVariable("id") Long id){
        CallResult res = null;
        try {
            res = moduleContentService.removeContent(id);
        } catch (Exception e) {
            logger.error("删除失败",e);
            res = CallResult.error("删除失败");
        }
        return res;
    }

    /**
     * 描   述：内容列表
     * 创 建 人：陈天龙
     * 日    期：2017/8/10
     * @param corpId  企业号
     * @param moduleType 模块类型
     * @return CallResult
     */
    @ApiOperation(value="内容列表", notes="内容列表",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "corpId", value = "企业号", required = true, dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "moduleType", value = "模板类型", required = true, dataType = "Long", paramType = "path" ),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Int", paramType = "query" ),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Int", paramType = "query" )})
    @GetMapping("/list/{corpId}/{moduleType}")
    public CallResult queryContent(@PathVariable("corpId") String corpId,@PathVariable("moduleType") String moduleType,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        CallResult res = null;
        try {
            res = moduleContentService.query(corpId,moduleType,pageNum,pageSize);
        } catch (Exception e) {
            logger.error("查询失败",e);
            res = CallResult.error("查询失败");
        }
        return res;
    }

    /**
     * 描   述：内容详情
     * 创 建 人：陈天龙
     * 日    期：2017/8/10
     * @return CallResult
     */
    @ApiOperation(value="内容详情", notes="内容详情",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "内容id", required = true, dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "corpId", value = "企业号", required = true, dataType = "String",paramType = "path")})
    @GetMapping("/detail/{corpId}/{id}")
    public CallResult contentDetail(@PathVariable("id") Long id,@PathVariable("corpId") String corpId){
        CallResult res = null;
        try {
            res = moduleContentService.getContentDetail(id,corpId);
        } catch (Exception e) {
            logger.error("查询失败",e);
            res = CallResult.error("查询失败");
        }
        return res;
    }

    /**
     * 描   述： 上移下移
     * 创 建 人：陈天龙
     * 日    期：2017/8/10
     * @return CallResult
     */
    @Access(accessType=AccessType.ADMIN)
    @ApiOperation(value="上移下移", notes="上移下移",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(name = "contentMoveVo", value = "移动操作类", required = true, dataType = "ContentMoveVo")
    @PutMapping("/move")
    public CallResult contentMove(@RequestBody ContentMoveVo contentMoveVo){
        CallResult res = null;
        try {
            res = moduleContentService.moveContent(contentMoveVo);
        } catch (Exception e) {
            logger.error("移动失败",e);
            res = CallResult.error("移动失败");
        }
        return res;
    }

    /**
     * 描   述：置顶
     * 创 建 人：陈天龙
     * 日    期：2017/8/10
     * @return CallResult
     */
    @Access(accessType=AccessType.ADMIN)
    @ApiOperation(value="置顶", notes="置顶",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "模块内容关系id", required = true, dataType = "Long", paramType = "path" ),
            @ApiImplicitParam(name = "moduleId", value = "模板id", required = true, dataType = "Long", paramType = "path" )})
    @PutMapping("/top/{moduleId}/{id}")
    public CallResult topContent(@PathVariable("id") Long id,@PathVariable("moduleId") Long moduleId){
        CallResult res = null;
        try {
            res = moduleContentService.topContent(id,moduleId);
        } catch (Exception e) {
            logger.error("置顶失败",e);
            res = CallResult.error("置顶失败");
        }
        return res;
    }
}
